var AuthRoleModel = $.extend(TradeModel, {
    selRow: null, 
    editor:{
	    title: "属性",
		winid:'AuthRole_EditForm',
		formid:'AuthRole_EditForm_fm',
		width:720,
		height:300
	},  
    postAction: 'add',
    url: '/sysmgr/authrole', 
    title: "标题",
    idField: "id",
    titleField: "roleName",
    toolbar: 'AuthRole_Toolbar',
    datagrid: 'AuthRole_Grid' 
}); 
 
AuthRoleModel.reload =function() {
	ReloadAuthRoleList();
	$('#AuthRole_EditForm').dialog('close'); 
} 


function fun_FindRowById(id) {
    var cRows = $('#AuthRole_Grid').datagrid('getRows');
    if (id == undefined || id == '' || id == null) return null;
    for (i = 0; i < cRows.length; i++) {
        if (cRows[i][AuthRoleModel.idField] == id) {
            return cRows[i];
        }
    }
    return null;
}




function ShowAuthRoleEditForm(id) {  
    AuthRoleModel.selRow = fun_FindRowById(id);
    if (AuthRoleModel.selRow == null) {
    	AuthRoleModel.selRow={};
        AuthRoleModel.postAction = 'add';
    }
    else {
        AuthRoleModel.postAction = 'update';
    } 
      
    $('#AuthRole_EditForm_fm').form('clear' );
    $('#AuthRole_EditForm_fm').form('load', AuthRoleModel.selRow);
    $('#AuthRole_EditForm').dialog({
        top: "50px",
        resizable: false,
        maximizable: false 
    })
    .dialog('open')
    .dialog('setTitle', (AuthRoleModel.postAction == 'Add') ? '新增' + AuthRoleModel.title : '修改' + AuthRoleModel.selRow[AuthRoleModel.titleField]);
}
function SaveAuthRole() {
	AuthRoleModel.submit();
	
}
function DelAuthRole( id,title) {
	AuthRoleModel.del(id,title);
} 

function AuthRoleSearchbox(value, name) {
    if (value == "" || value == null) {
        $.messager.alert('提示', '请输入关键字!', 'info');
    }
    else {
        $('#AuthRole_Grid').datagrid('reload');
    }

}
 
function ReloadAuthRoleList() {
    $('#AuthRole_Grid').datagrid('reload');
}
 
function formatter_roletype(value, rec) { 
	var html=''; 
	if(value == null ){
		html='<span style="color:Red">未知</span>' ;
	}
	else if(value.trim() == 'F'){
		html='<span style="color:#216499">功能</span>'; 
	}else if(value.trim() == 'D'){
		html='<span style="color:#216499">数据</span>'; 
	}else{
		html='<span style="color:Red">未知</span>' ;
	}
    return  html;
}

$(function () {   
    $('#AuthRole_Grid').datagrid({
        pageSize: 20,
        fit: true,
        singleSelect: true,
        checkbox: false,
        rownumbers: false,
        fitColumns: true,
        pagination: true, 
        method: 'post',
        idField: AuthRoleModel.idField,
        treeField: AuthRoleModel.titleField,
        url: Onluy.basePath()+AuthRoleModel.url+'/list',
        loader:AuthRoleModel.Loader,
        toolbar: '#AuthRole_Toolbar', 
        columns: [
            [ 
        { field: 'roleName', title: '角色名称', width:100, align: 'left',sortable:true },  
        { field: 'roleMemo', title: '角色说明', width:100, align: 'left',sortable:true },  
        { field: 'roleType', title: '角色类型', width:100, align: 'left',sortable:true , formatter: formatter_roletype},  
        { field: 'roleState', title: '是否生效', width:100, align: 'left',sortable:true, formatter: formatter_IsNo }, 
        { field: 'createUser', title: '创建人', width:100, align: 'left' ,sortable:true},  
        { field: 'createDepart', title: '创建部门', width:100, align: 'left' ,sortable:true},  
        { field: 'createDate', title: '创建日期', width:100, align: 'left' ,sortable:true},  
			 {
					    field: 'opt', title: '操作', width: 100, align: 'center',  
					    formatter: function (value, rec) {
					        return '<img class="btn-16" src="'+Onluy.basePath()+'/Images/icos/16/pencil.gif" onclick="ShowAuthRoleEditForm(\'' + rec[AuthRoleModel.idField] + '\');"   />'
                                + '<img class="btn-16" src="'+Onluy.basePath()+'/Images/icos/16/delete.gif" onclick="DelAuthRole(\'' + rec[AuthRoleModel.idField] + '\',\'' + rec[AuthRoleModel.titleField] + '\');"  />';
					    }
					} 
            ]
        ],

        
        onLoadError: function (res) {
        	AuthRoleModel.ShowException(res); 
        },
        onLoadSuccess: function (data) {
             if(data.success){
        		
        	}
        	else{
        		 $.messager.alert('提示', data.msg );
        	} 
        },
        onDblClickRow: function (rowIndex, rowData) {
            ShowAuthRoleEditForm(rowData[AuthRoleModel.idField]);
        },
        onBeforeLoad: function (param) { 
            param['catchKey'] = Math.random();
            param['name'] = $('#AuthRole_ss').searchbox('getName');
            param['value'] = $('#AuthRole_ss').searchbox('getValue');
        }
    });
    //设置分页控件  
    var p = $('#AuthRole_Grid').datagrid('getPager');
    $(p).pagination({
        pageSize: 20, //每页显示的记录条数，默认为10  
        pageList: [20, 30, 50, 100], //可以设置每页记录条数的列表  
        beforePageText: '第', //页数文本框前显示的汉字  
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
        onBeforeRefresh: function () {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });

});



